//
//  Account.m
//  RRQS
//
//  Created by kyjun on 16/3/30.
//
//

#import "Account.h"
#import "AccountDetail.h"
#import "ATM.h"

@interface Account ()

@property(nonatomic,strong) UIView* headerView;
@property(nonatomic,strong) UIImageView* iconMoney;
@property(nonatomic,strong) UILabel* labelTitle;
@property(nonatomic,strong) UILabel* labelMoney;
@property(nonatomic,strong) UIButton* btnDeail;
@property(nonatomic,strong) UIButton* btnAppCash;

@end

@implementation Account

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = theme_default_color;
    self.title = @"账户余额";
    [self layoutUI];
    [self layoutConstraints];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryData) name:NotifciationReLoadUserInfo object:nil];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.labelMoney.text = [NSString stringWithFormat:@"%.2f",[self.Identity.userInfo.account floatValue]];
}



#pragma mark =====================================================  user interface layout
-(void)layoutUI{
//     [self.view addSubview:self.btnAppCash];
    [self.view addSubview:self.headerView];


    [self.headerView addSubview:self.iconMoney];
    [self.headerView addSubview:self.labelTitle];
    [self.headerView addSubview:self.labelMoney];
    [self.headerView addSubview:self.btnDeail];
}

-(void)layoutConstraints{
    
    
    
//    [self.btnAppCash mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(50.f);
//        make.right.equalTo(self.view);
//        make.left.equalTo(self.view);
//        make.bottom.equalTo(self.view);
//    }];
    
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.right.equalTo(self.view);
        make.left.equalTo(self.view);
//        make.bottom.equalTo(self.btnAppCash.mas_top);
        make.bottom.equalTo(self.view);

    }];
    
    
    
    [self.iconMoney mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(100.0);
        make.size.mas_equalTo(CGSizeMake(75, 75));
    }];
    
    
    
    [self.labelMoney mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.iconMoney.mas_bottom).mas_offset(20);
        make.width.equalTo(self.headerView);
        make.left.equalTo(self.headerView);
        make.height.mas_equalTo(40);
        
    }];
    
    
    [self.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labelMoney.mas_bottom);
        make.left.right.equalTo(self.labelMoney);
        make.height.equalTo(self.labelMoney);
    }];
  
    
    [self.btnDeail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labelTitle.mas_bottom).mas_offset(20);
        make.left.right.equalTo(self.headerView);
        make.height.equalTo(self.labelTitle);
    }];
    
   
}

#pragma mark =====================================================  DataSource
-(void)queryData{
    AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];
    NSDictionary* arg = @{@"ince":@"get_emp_info",@"emp_id":self.Identity.userInfo.userID};
    [mgr POST:kHost parameters:arg progress:nil success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject) {
        [WMHelper outPutJsonString:responseObject];
        NSInteger flag = [[responseObject objectForKey:@"status"]integerValue];
        if(flag == 1){
            NSDictionary* emptyDict = [responseObject objectForKey:@"info"];
            if(![WMHelper isNULLOrnil:emptyDict]){
                MMember* item = self.Identity.userInfo;
                item.dataLine = [emptyDict objectForKey:@"dataline"];
                item.status = [emptyDict objectForKey:@"status"];
                item.account = [emptyDict objectForKey:@"account"];
                item.lat = [emptyDict objectForKey:@"lat"];
                item.lng = [emptyDict objectForKey:@"lng"];
                item.close = [emptyDict objectForKey:@"close"];
                
                Boolean flag= [NSKeyedArchiver archiveRootObject:item toFile:[WMHelper archiverPath]];
                if(flag){
                    self.labelMoney.text = self.Identity.userInfo.account;
                }else
                    NSLog(@"%@",@"归档失败!");
            }
            
        }else{
            [self alertHUD:[responseObject objectForKey:@"fail"]];
        }

    } failure:^(NSURLSessionDataTask *_Nonnull operation, NSError * _Nonnull error) {
        [self alertHUD:@"网络异常"];
    }];
    
}

#pragma mark =====================================================  SEL
-(void)detailTouch:(UIButton *)sender{
    AccountDetail* controller = [[AccountDetail alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}
-(void)appCashTouch:(UIButton *)sender{
    ATM* controller = [[ATM alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark =====================================================  property package 
-(UIView *)headerView{
    if(!_headerView){
        _headerView = [[UIView alloc]init];
        _headerView.backgroundColor = [UIColor whiteColor];
        
    }
    return _headerView;
}

-(UIImageView *)iconMoney{
    if(!_iconMoney){
        _iconMoney = [[UIImageView alloc]init];
        [_iconMoney setImage:[UIImage imageNamed:@"icon-cash"]];
    }
    return _iconMoney;
}

-(UILabel *)labelTitle{
    if(!_labelTitle){
        _labelTitle = [[UILabel alloc]init];
        _labelTitle.text = @"余额总数(元)";
        _labelTitle.textAlignment = NSTextAlignmentCenter;
        _labelTitle.textColor = [UIColor colorWithRed:64/255.f green:64/244.f blue:64/255.f alpha:1.0];
        _labelTitle.font = [UIFont systemFontOfSize:17.f];
    }
    return _labelTitle;
}

-(UILabel *)labelMoney{
    if(!_labelMoney){
        _labelMoney = [[UILabel alloc]init];
        _labelMoney.textAlignment = NSTextAlignmentCenter;
        _labelMoney.textColor = [UIColor colorWithRed:248/255.f green:162/244.f blue:48/255.f alpha:1.0];
        _labelMoney.font = [UIFont boldSystemFontOfSize:24.f];
    }
    return _labelMoney;
}

-(UIButton *)btnDeail{
    if(!_btnDeail){
        _btnDeail = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnDeail setTitleColor:[UIColor colorWithRed:248/255.f green:162/244.f blue:48/255.f alpha:1.0] forState:UIControlStateNormal];
        _btnDeail.titleLabel.font =[UIFont systemFontOfSize:17.f];
        [_btnDeail setTitle:@"查看余额明细" forState:UIControlStateNormal];
        _btnDeail.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [_btnDeail addTarget:self action:@selector(detailTouch:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnDeail;
}

-(UIButton *)btnAppCash{
    if(!_btnAppCash){
        _btnAppCash = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnAppCash setTitleColor:[UIColor colorWithRed:64/255.f green:64/244.f blue:64/255.f alpha:1.0] forState:UIControlStateNormal];
        [_btnAppCash setTitle:@"申请提现" forState:UIControlStateNormal];
        _btnAppCash.titleLabel.font =[UIFont systemFontOfSize:17.f];
        CALayer* border = [[CALayer alloc]init];
        border.frame = CGRectMake(0, 0.f, SCREEN_WIDTH, 1.f);
        border.backgroundColor = [UIColor colorWithRed:230/255.f green:230/255.f blue:230/255.f alpha:1.0].CGColor;
        [_btnAppCash.layer addSublayer:border];
        [_btnAppCash addTarget:self action:@selector(appCashTouch:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _btnAppCash;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
